import 'package:flutter/material.dart';
import 'dart:async';

import 'package:robot3/widgets.dart'; // For Future

void main() {
  runApp(const MaterialApp(home: FutureBuilderExample()));
}

class FutureBuilderExample extends StatelessWidget {
  const FutureBuilderExample({super.key});

  // Simulating a network call with a Future
  Future<String> fetchData() async {
    await Future.delayed(Duration(seconds: 5)); // Simulate network delay
    return "Hello, FutureBuilder!"; // Simulated response
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("FutureBuilder Example")),
      body: Center(
        child: FutureBuilder<String>(
          future: fetchData(), // The future to wait on
          builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
            if (snapshot.connectionState == ConnectionState.waiting) {
              // While the future is running
              return CircularProgressIndicator(color:Colors.blue);
            } else if (snapshot.hasError) {
              // If the future fails
              return Text("Error: ${snapshot.error}");
            } else if (snapshot.hasData) {
              // When the future completes successfully
              return Text(
                snapshot.data!,
                style: TextStyle(fontSize: 24, color: Colors.green),
              );
            } else {
              // For other cases (rare)
              return Text("No data available");
            }
          },
        ),
      ),
    );
  }
}
